package com.wolfalpha.lwtool.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.wolfalpha.lwtool.R;
import com.wolfalpha.lwtool.constant.Application;
import com.wolfalpha.lwtool.constant.Constant;
import com.wolfalpha.lwtool.service.MyService;
import com.wolfalpha.lwtool.service.UserService;
import com.wolfalpha.lwtool.service.impl.UserServiceImpl;

/**
 * 用户登录
 * 
 * @author ttStudio
 * 
 */
public class LoginActivity extends Activity {

	private EditText userid;
	private EditText password;
	private TextView btn_login;
	private LoginTask mTask;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		// 设置当前活动状态
		Constant.initCitys();
		Application.setFrontActivity(Application.N_LOGIN);
		initView();
		initListeners();
		
//		Intent service = new Intent(LoginActivity.this,MyService.class);
//		startService(service);
	}

	private void initListeners() {
		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					mTask = new LoginTask();
					mTask.execute();
				} catch (Exception e) {

				}
			}
		});
	}

	private void initView() {
		userid = (EditText) findViewById(R.id.idcard);
		password = (EditText) findViewById(R.id.password);
		btn_login = (TextView) findViewById(R.id.btnLogin);

		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(LoginActivity.this);
		if (sp.contains("id")) {
			userid.setText(sp.getString("id", ""));
			password.setText(sp.getString("pwd", ""));
		}

		pd = new ProgressDialog(this);
	}

	private class LoginTask extends AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			pd.setMessage("正在验证并登录");
			pd.setCancelable(false);
			pd.show();
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			try {
				SharedPreferences sp = PreferenceManager
						.getDefaultSharedPreferences(LoginActivity.this);
				Editor ed = sp.edit();
				ed.putString("id", userid.getText().toString());
				ed.putString("pwd", password.getText().toString());
				ed.commit();
				if (userid.getText().toString().equals("")
						|| password.getText().toString().equals("")) {
					this.cancel(true);
				}
				UserService sav = new UserServiceImpl();
				int result = sav.login(userid.getText().toString(), password
						.getText().toString());
				if (result != 1) {
					System.out.println("Login Fail");
					this.cancel(true);
				}

				/** JPush **/
				JPushInterface.setDebugMode(true);
				JPushInterface.init(LoginActivity.this);

			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("Login Fail");
				this.cancel(true);
			}
			return null;
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			startActivity(new Intent(LoginActivity.this, MainActivity.class));
			pd.dismiss();
		}

		// onCancelled方法用于在取消执行中的任务时更改UI
		@Override
		protected void onCancelled() {
			Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT)
					.show();
			pd.dismiss();
		}
	}
}
